﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace ConsoleApp5
{

    class Program
    {
        class NameSort : IComparer<Student>
        {
            public int Compare(Student x, Student y)
            {
                return x.Num.CompareTo(y.Num);
            }
        }
        class NumSort : IComparer<Student>
        {
            public int Compare(Student x, Student y)
            {
                return x.Num.CompareTo(y.Num);
            }
        }
        class AgeSort : IComparer<Student>
        {
            public int Compare(Student x, Student y)
            {
                return x.Age.CompareTo(y.Age);
            }
        }
        static void Main(string[] args)
        {
            //添加3个类，分别实现 IComparer<T>接口，实现对Student类的三个字段的排序。
            //1、学生类：学号、姓名、年龄
            //2、请选择：1、添加学生信息。2、查询学生信息。
            //3、重复的学号不能添加。
            //4、查询学生信息功能中有：1、查询所有（按学号排序）2、查询所有（按姓名排序），
            //2、查询所有（按年龄排序）4、按学号查询（查没有，则打印查无此学生）5、退出
            Student s1 = new Student("张1", 18, 1);
            Student s2 = new Student("张2", 19, 2);
            Student s3 = new Student("张3", 20, 3);
            List<Student> list = new List<Student>();
            list.Add(s1);
            list.Add(s2);
            list.Add(s3);
            Hashtable ht = new Hashtable();
            ht.Add(1, "张1" + " " + 18 + " " + 1);
            ht.Add(2, "张2" + " " + 19 + " " + 2);
            ht.Add(3, "张3" + " " + 20 + " " + 3);
            Home(list, ht);

        }

        private static void Home(List<Student> list, Hashtable ht)
        {
            


            Console.WriteLine("请选择：1、添加学生信息。2、查询学生信息");
            int input = int.Parse(Console.ReadLine());

            switch (input)
            {
                case 1:
                    DupChe(list, ht);
                    Home(list, ht);
                    break;
                case 2:
                    query(list, ht);
                    break;

                default:

                    break;
            }
        }

        private static void query(List<Student> list, Hashtable ht)
        {
            Console.WriteLine("1、查询所有（按学号排序）2、查询所有（按姓名排序），3、查询所有（按年龄排序）4、按学号查询（查没有，则打印查无此学生）5、退出");
            int input = int.Parse(Console.ReadLine());

            switch (input)
            {
                case 1:
                    
                    list.Sort(new NumSort());
                    Console.WriteLine("===============");
                    foreach (var item in list)
                    {
                        Console.WriteLine(item);
                    }
                    break;
                case 2:
                    list.Sort(new NameSort());
                    Console.WriteLine("===============");
                    foreach (var item in list)
                    {
                        Console.WriteLine(item);
                    }
                    break;
                    case 3:
                    list.Sort(new AgeSort());
                    Console.WriteLine("===============");
                    foreach (var item in list)
                    {
                        Console.WriteLine(item);
                    }
                    break;
                case 4:
                    Console.WriteLine("输入学号");
                    int num = int.Parse(Console.ReadLine());

                    if (ht.Contains(num))
                    {
                        Console.WriteLine(ht[num]);
                    }
                    else
                    {
                        Console.WriteLine("查没有，则打印查无此学生");
                    }
                    break;
                case 5:
                    Console.WriteLine("退出中...");
                    break;
                default:

                    break;
            }




        }

        private static void DupChe(List<Student> list, Hashtable ht)
        {
            Console.WriteLine("输入名字");
            string Name = Console.ReadLine();
            Console.WriteLine("输入学号");
            int Num = int.Parse(Console.ReadLine());
            Console.WriteLine("输入年龄");
            int Age = int.Parse(Console.ReadLine());
            
            Student student =new Student(Name,Num,Age);

            if (ht.ContainsKey(Num))
            {
                Console.WriteLine("已存在该学号");
            }
            else
            {
                ht.Add(Num, Num);
                list.Add(student);
                ht.Add(Num, Name + " " + Age + " " + Num);
                Console.WriteLine("添加成功");
            }

        }
    }
}
